package cn.edu.cqie.maven_onlinexem.controller;

import cn.edu.cqie.maven_onlinexem.bean.question;
import cn.edu.cqie.maven_onlinexem.service.QuestionService;
import cn.edu.cqie.maven_onlinexem.service.SubjectService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class QuestionController {
    @Resource
    QuestionService questionService;
    SubjectService subjectService;
    @RequestMapping("Upload_question")
    public ModelAndView Upload_question(){
        ModelAndView modelAndView = new ModelAndView("Upload_question");
        return modelAndView;
    }
    @RequestMapping("main")
    public ModelAndView main(){
        ModelAndView modelAndView = new ModelAndView("main");
        return modelAndView;
    }
    @RequestMapping("Select_question")
    public ModelAndView Select_question(){
        ModelAndView modelAndView = new ModelAndView("Select_question");
        return modelAndView;
    }
    @ResponseBody
    @RequestMapping("doUpload_question")
    public Object doAjaxLogin(question question, HttpSession session, HttpServletResponse response){
        System.out.println(question.getSubject_name());
        System.out.println(question.getQuestion_answer());
        if(subjectService.findSubject_id(question.getSubject_name())==0){
            System.out.println("1");
        }
        else{
            System.out.println("0");
        }
        question.setSubject_id(subjectService.findSubject_id(question.getSubject_name()));
        questionService.insertQuestion(question);
        boolean b = questionService.findQuestion_name(question.getQuestion_name());
        String view="";
        if (b){
            view="Select_question";
        }
        else {
            view="Upload_question";
        }
        ModelAndView modelAndView = new ModelAndView(view);
        return modelAndView;
    }
}
